/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import entities.ContentEntity;
import entities.ContentFavoritesEntity;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author vasi
 */
@Stateless
public class ContentFavoritesEntityFacade implements ContentFavoritesEntityFacadeLocal {
    @PersistenceContext(unitName = "AggregatorClient-ejbPU")
    private EntityManager em;

    public void create(ContentFavoritesEntity contentFavoritesEntity) {
        em.persist(contentFavoritesEntity);
    }

    public void edit(ContentFavoritesEntity contentFavoritesEntity) {
        em.merge(contentFavoritesEntity);
    }

    public void remove(ContentFavoritesEntity contentFavoritesEntity) {
        em.remove(em.merge(contentFavoritesEntity));
    }

    public ContentFavoritesEntity find(Object id) {
        return em.find(ContentFavoritesEntity.class, id);
    }

    public ContentFavoritesEntity findByUidAndCid(Long uid, Long cid) {
        Query query = em.createQuery("SELECT content_favorites FROM ContentFavoritesEntity content_favorites " +
                " WHERE content_favorites.uid=:uid AND content_favorites.cid=:cid", ContentFavoritesEntity.class);
        query.setParameter("uid", uid);
        query.setParameter("cid", cid);
        if (query.getResultList().size() > 0){
            return (ContentFavoritesEntity)query.getSingleResult();
        }
        return null;
    }

    public List<ContentFavoritesEntity> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(ContentFavoritesEntity.class));
        return em.createQuery(cq).getResultList();
    }

    public List<ContentFavoritesEntity> findRange(int[] range) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(ContentFavoritesEntity.class));
        Query q = em.createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }


    public List<ContentEntity> findFullContentsByRange(int[] range, Long uid) {
        Query q = em.createQuery("SELECT content FROM ContentEntity content, ContentFavoritesEntity content_favorites" +
                " WHERE content.cid=content_favorites.cid AND content_favorites.uid=:uid", ContentEntity.class);
        q.setParameter("uid", uid);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public List<ContentEntity> findAllFullContents(Long uid) {
        Query q = em.createQuery("SELECT content FROM ContentEntity content, ContentFavoritesEntity content_favorites" +
                " WHERE content.cid=content_favorites.cid AND content_favorites.uid=:uid", ContentEntity.class);
        q.setParameter("uid", uid);
        return q.getResultList();
    }

    public int count() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<ContentFavoritesEntity> rt = cq.from(ContentFavoritesEntity.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

}
